function hasPermission(one_per){
	var perms = new Array();
	if(one_per){
		perms.push(one_per);
	}else{
		var prs = document.getElementsByName("p_list");
		for(var i = 0; i < prs.length; i++){
			if(prs[i].checked){
				perms.push(prs[i].value);
			}
		}
	}
	var rets = api.hasPermission({
		list:perms
	});
	if(!one_per){
		apialert('判断结果：' + JSON.stringify(rets));
		return;
	}
	return rets;
}

function reqPermission(one_per, callback){
	var perms = new Array();
	if(one_per){
		perms.push(one_per);
	}
	api.requestPermission({
		list: perms,
		code: 100001
	}, function(ret, err){
		if(callback){
			callback(ret);
			return;
		}
		var str = '请求结果：\n';
		str += '请求码: ' + ret.code + '\n';
		str += "是否勾选\"不再询问\"按钮: " + (ret.never ? '是' : '否') + '\n';
		str += '请求结果: \n';
		var list = ret.list;
		for(var i in list){
			str += list[i].name + '=' + list[i].granted + '\n';
		}
		console.log(JSON.stringify(ret));
	});
}

function reqPermissionarr(perms, callback){
	api.requestPermission({
		list: perms,
		code: 100001
	}, function(ret, err){
		if(callback){
			callback(ret);
			return;
		}
		var str = '请求结果：\n';
		str += '请求码: ' + ret.code + '\n';
		str += "是否勾选\"不再询问\"按钮: " + (ret.never ? '是' : '否') + '\n';
		str += '请求结果: \n';
		var list = ret.list;
		for(var i in list){
			str += list[i].name + '=' + list[i].granted + '\n';
		}
		console.log(JSON.stringify(ret));
	});
}

function confirmPerarr(permarr, permdesc, callback){
	var hasperm = 1;
	for(var index in permarr){
		var perm = permarr[index];
		var has = hasPermission(perm);
		if(!has || !has[0] || !has[0].granted){
			hasperm = 0;
			break;
		}
	}
	
	if(hasperm == 0){
		api.confirm({
			title: '提醒',
			msg: '该功能需要开启 “' + permdesc + '” 权限\n是否前往设置？',
			buttons: ['去设置', '取消']
		}, function(ret, err) {
			if(1 == ret.buttonIndex){
				reqPermissionarr(permarr, callback);
			}
		});
		return false;
	}
	return true;
}

function confirmPer(perm, permdesc, callback){
	var has = hasPermission(perm);
	if(!has || !has[0] || !has[0].granted){
		api.confirm({
			title: '提醒',
			msg: '该功能需要开启 “' + permdesc + '” 权限\n是否前往设置？',
			buttons: ['去设置', '取消']
		}, function(ret, err) {
			if(1 == ret.buttonIndex){
				reqPermission(perm, callback);
			}
		});
		return false;
	}
	return true;
}

function checkPer(perm){
	var has = hasPermission(perm);
	if(!has || !has[0] || !has[0].granted){
		return false;
	}
	return true;
}